﻿using System;

namespace BizElements.Core
{
    /// <summary>Entity DAO creation strategy/algorithm that returns an instance of class specified in generic <b>TEntityDAO</b> argument.</summary>
    /// <typeparam name="TEntityDAO">Type of entity DAO objects returned by <b>GetEntityDAO</b> method.</typeparam>
    public sealed class CustomEntityDaoCreationStrategy<TEntityDAO> : IEntityDaoCreationStrategy
        where TEntityDAO : IEntityDAO, new()
    {
        /// <summary>Creates a new instance of class specified in <b>TEntityDAO</b> generic argument.</summary>
        /// <param name="entity">Entity that must be compatibile with the created DAO.</param>
        /// <param name="conn">Connection context the DAO will use. If null, default DAO settings are used.</param>
        /// <returns><see cref="IEntityDAO"/> object.</returns>
        public IEntityDAO GetEntityDAO(IEntity entity, IConnectionProvider conn)
        {
            IEntityDAO dao = new TEntityDAO();
            dao.Entity = entity;
            if (conn != null)
                dao.ConnectionProvider = conn;

            return dao;
        }
    }
}
